<?php
namespace Qgs\Controller;

use Common\Model\DiscountCouponModel;
use Common\Model\StoreProductModel;

/**
 * 订单促销
 * Class OrderPromotionController
 * @package Qgs\Controller
 */

class OrderPromotionController extends BaseController {
    public $local_id;

    public function __construct()
    {
        parent::__construct();
        $this->local_id = session('userInfo')['local_id'];
    }

    /**
     * 列表页
     */
    public function index(){
        $time = date('Y-m-d H:i:s', time());
        $type = I('type');
        if ($type == 'timeout') {
            $where['end_time'] = ['LT', $time];
        } else {
            $where['end_time'] = ['EGT', $time];
        }

        // 排序
        $order = I('param.order', 'desc');
        $order == 'asc' ? 'desc' : 'asc';
        $sunxu = $order == 'asc' ? 'desc' : 'asc';
        $this->assign(['order' => $sunxu]);
        $order = ['id' => $order];

        $where['local_id'] = $this->local_id;

        $promotion = M('b2c_store_order_promotion');

        $count = $promotion->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出

        $promotion_msg = $promotion->where($where)->order($order)->limit($Page->firstRow . ',' . $Page->listRows)->select();

        foreach ($promotion_msg as &$v) {
            $v['type_question_msg'] = $this->type_question_msg($v['type_question']);
            $v['type_result_msg'] = $this->type_result_msg($v['type_result']);
        }


        $this->assign([
            'msg' => $promotion_msg,
            'show' => $show
        ]);
        $this->display();
    }

    /**
     * 促销条件
     */
    public function type_question_msg($i)
    {
        switch ($i) {
            case 1:
                $msg = '订单金额满 X 时，对指定商品促销';
                break;
            case 2;
                $msg = '订单金额满 X 时，对所有订单促销';
                break;
            case 3;
                $msg = '所有订单';
                break;
        }
        return $msg;
    }
    /**
     * 促销结果
     */
    public function type_result_msg($i){
        switch ($i) {
            case 1:
                $msg = '订单免运费';
                break;
            case 2;
                $msg = '订单减去X价格';
                break;
            case 3;
                $msg = '送优惠卷';
                break;
        }
        return $msg;
    }

    /**
     * 添加
     */
    public function add(){
        if (!IS_POST) {
            // 优惠券
            $b2c_discount_coupon = new DiscountCouponModel();
            $discount_coupon_msg = $b2c_discount_coupon->getDiscountCoupon();
            $this->assign(['discount_coupon_msg' => $discount_coupon_msg]);
            $this->display();
        }else{
            $name = I("name"); //名称
            $description = I("description"); //说明
            $paita = I("paita"); //排他功能
            $display = I("display"); //是否启用
            $sort = I("sort"); //是否启用
            $type_question = I("type_question"); //促销条件,1：订单金额满 X 时，对指定商品促销，2：订单金额满 X 时，对所有订单促销，3：所有订单
            $type_result = I("type_result"); //促销结果 1：订单免运费，2：订单减去X价格，3：送优惠卷
            $question_man_price = I("question_man_price"); //type_question=1,订单满x元
            $question_include = I("question_include"); //type_question=1,是否包含
            $question_goods_id_array = I("question_goods_id_array"); //type_question=1,商品id
            $question_man_price_two = I("question_man_price_two"); //type_question=1,订单满x元
            $result_price = I("result_price"); //type_result=2,减去x元
            $result_discount_coupon = I("result_discount_coupon"); //type_result=3,送优惠卷
            $start_time = I("start_time");
            $end_time = I("end_time");

            if (strtotime($start_time) >= strtotime($end_time)) {
                $this->error('开始时间不能大于结束时间');
            }
            $time = date("Y-m-d H:i:s", time());

            $data['local_id'] = $this->local_id;
            $data['name'] = $name;
            $data['description'] = $description;
            $data['paita'] = $paita;
            $data['display'] = $display;
            $data['sort'] = $sort;
            $data['type_question'] = $type_question;// 促销条件

            if ($type_question ==1) {
                $data['question_include'] = $question_include;
                $data['question_man_price'] = $question_man_price;
                $data['question_goods_id_array'] = $question_goods_id_array;
            }
            if ($type_question == 2){
                $data['question_man_price_two'] = $question_man_price_two;
            }


            $data['type_result'] = $type_result; //促销结果
            if($type_result ==2) {
                $data['result_price'] = $result_price;
            }
            if ($type_result == 3){
                $data['result_discount_coupon'] = implode('#',$result_discount_coupon);
            }
            $data['start_time'] = $start_time;
            $data['end_time'] = $end_time;
            $data['created_at'] = $time;

            $promotion = M('b2c_store_order_promotion')->add($data);

            if ($promotion) {
                $this->success('添加成功', U('/Qgs/OrderPromotion/index'));
            } else {
                $this->error('添加失败', U('/Qgs/OrderPromotion/index'));
            }
        }
    }

    /**
     * 修改
     */
    public function update(){
        $id = I('id');
        $promotion = M('b2c_store_order_promotion');
        $time = date('Y-m-d H:i:s', time());
        $where['end_time'] = ['EGT', $time];
        $where['id'] = $id;
        $where['local_id'] = $this->local_id;
        $promotion_msg = $promotion->where($where)->find();
        if (empty($promotion_msg)) {
            $this->error('有误', U('OrderPromotion/index'));
        }

        if ($promotion_msg['type_question'] == '1') {  // 查询商品信息
            $array_goods_id = explode('#', $promotion_msg['question_goods_id_array']);
            $array_goods_id = implode(',', $array_goods_id);
//            $products = M('b2c_products a')->field('a.product_id,a.name,a.spec_info,b.store_pid')
//                ->join(C('DB_PREFIX') . 'b2c_store_product b on a.product_id=b.product_id', 'left')
//                ->where(['b.store_pid' => ['in', $array_goods_id]])
//                ->select();
            $store_products = new StoreProductModel();
            $products = $store_products->getProduct('a.product_id,a.name,a.spec_info,b.store_pid', ['b.store_pid' => ['in', $array_goods_id]]);

            $this->assign(['products' => $products]);
        }

        if ($promotion_msg['type_result'] == '3') {
            $result_discount_coupon = explode('#',$promotion_msg['result_discount_coupon']);
            $this->assign(['result_discount_coupon' => $result_discount_coupon]);
        }

        //优惠券
        $b2c_discount_coupon = new DiscountCouponModel();
        $discount_coupon_msg = $b2c_discount_coupon->getDiscountCoupon();

        $this->assign(['discount_coupon_msg' => $discount_coupon_msg]);

        $this->assign(['msg' => $promotion_msg]);
        $this->display();
    }

    /**
     * 修改保存
     */
    public function save(){
        if (IS_POST) {
            $id = I('id');

            $promotion = M('b2c_store_order_promotion');
            $time = date('Y-m-d H:i:s', time());
            $where['end_time'] = ['EGT', $time];
            $where['id'] = $id;
            $where['local_id'] = $this->local_id;
            $promotion_msg = $promotion->where($where)->find();
            if (empty($promotion_msg)) {
                $this->error('有误');
            }

            $name = I("name"); //名称
            $description = I("description"); //说明
            $paita = I("paita"); //排他功能
            $display = I("display"); //是否启用
            $sort = I("sort"); //是否启用
            $type_question = I("type_question"); //促销条件,1：订单金额满 X 时，对指定商品促销，2：订单金额满 X 时，对所有订单促销，3：所有订单
            $type_result = I("type_result"); //促销结果 1：订单免运费，2：订单减去X价格，3：送优惠卷
            $question_man_price = I("question_man_price"); //type_question=1,订单满x元
            $question_include = I("question_include"); //type_question=1,是否包含
            $question_goods_id_array = I("question_goods_id_array"); //type_question=1,商品id
            $question_man_price_two = I("question_man_price_two"); //type_question=1,订单满x元
            $result_price = I("result_price"); //type_result=2,减去x元
            $result_discount_coupon = I("result_discount_coupon"); //type_result=3,送优惠卷
            $start_time = I("start_time");
            $end_time = I("end_time");

            if (strtotime($start_time) >= strtotime($end_time)) {
                $this->error('开始时间不能大于结束时间');
            }
            $time = date("Y-m-d H:i:s", time());

            $data['name'] = $name;
            $data['description'] = $description;
            $data['paita'] = $paita;
            $data['display'] = $display;
            $data['sort'] = $sort;
            $data['type_question'] = $type_question;// 促销条件

            if ($type_question ==1) { // 促销条件,1：订单金额满 X 时，对指定商品促销
                $data['question_include'] = $question_include;
                $data['question_man_price'] = $question_man_price;
                $data['question_goods_id_array'] = $question_goods_id_array;
            }
            if ($type_question == 2){ // 促销条件2：订单金额满 X 时，对所有订单促销
                $data['question_man_price_two'] = $question_man_price_two;
            }


            $data['type_result'] = $type_result; //促销结果
            if($type_result ==2) {
                $data['result_price'] = $result_price;
            }
            if ($type_result == 3){
                $data['result_discount_coupon'] = implode('#',$result_discount_coupon);
            }
            $data['start_time'] = $start_time;
            $data['end_time'] = $end_time;
            $data['created_at'] = $time;

            $promotion = M('b2c_store_order_promotion')->where(['id' => $id, 'local_id' => $this->local_id])->save($data);
            if ($promotion) {
                $this->success('已保存',U('/Qgs/OrderPromotion/index'));
            }else{
                $this->error('保存失败');
            }
        }
    }

    public function delete()
    {
        $id = I('id');
        $promotion = M('b2c_store_order_promotion');

        $time = date('Y-m-d H:i:s', time());

        $where['end_time'] = ['LT', $time];
        $where['id'] = $id;
        $where['local_id'] = $this->local_id;

        $discount_coupon_msg = $promotion->where($where)->find();
        if (empty($discount_coupon_msg)) {
            $this->error('该订单促销时间还没到，不能删除', U('Qgs/OrderPromotion/index'));exit;
        }

        $promotion->where(['id' => $id])->delete();
        $this->success('成功删除');
    }




    /**
     * 添加产品
     */
    public function productAdd()
    {

        $name = I('name', '', htmlspecialchars());
        $id = I('id', '', htmlspecialchars());

        if (!empty($name)) {
            $where['b.name'] = array('like', "%$name%");
        }

        $page = max(I('get.p'), 1);
        $num = max(I('get.num'), 20);

        $order = "a.store_pid desc";
        $product_relation_model = M('b2c_store_product a');

        $where['a.local_id'] = $this->local_id;
        $where['a.store_status'] = 'true';
        $where['b.disabled'] = 'false';

        $field = "a.marketprice,a.price,a.store,a.discount,a.store_pid,b.product_id,b.goods_id,b.title,b.name,b.spec_info,a.local_id";

        // 可售货品
        $count = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id')
            ->where($where)
            ->count();

        $Page = new \Think\Page($count, $num);
        $show = $Page->show();
        $product_relation_msg = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id', 'left')
            ->where($where)
            ->order($order)
            ->field($field)
            ->page($page, $num)
            ->select();

//        $menu = M('store_carousel')->where(['id' => $id])->find();
//        $value = unserialize($menu['value']);

        $this->assign([
            'product_relation_msg' => $product_relation_msg,
            'show' => $show,
            'id' => $id,
//            'value' => $value
        ]);

        $this->display();
    }




}